export default {
	data() {
		return {
			typeStr:{
				'1': '判断题',
				'2': '单选题',
				'3': '多选题',
				'4': '填空题',
				'5': '问答题'
			},
			currentType: 0, //当前题型
			subjectIndex: 0, //跳转索引,也表示当前第几题，从0开始
			// 数据组成：题目，类型，选项，答案，是否回答，是否收藏，答案解析
			subjectList: null,
			dataFlag: false,
			// 表示无题可审核
			noSubject: false
		}
	},
	onShow() {
		this.getSubject();
	},
	onPullDownRefresh() {
		this.getSubject();
		uni.stopPullDownRefresh();
	},
	methods: {
		getSubject: function() {
			this.dataFlag = false;

			// callback方式
			uniCloud.callFunction({
				name: 'getTestSubject',
				data: {},
				success: (res) => {
					this.showSubject(res.result.data);
				},
				fail: (res) => {
					console.log('fail:' + res);
				},
				complete: (res) => {
					console.log('complete:' + res);
				}
			});
		},
		showSubject: function(data) {
			// 初始化第一题
			if (data[0]) {
				this.currentType = data[0].type;
				this.noSubject = false;
			} else {
				this.noSubject = true;
			}
			this.subjectList = data;
			this.dataFlag = true;
		},
		ShowToast(msg) {
			uni.showToast({
				title: msg,
				duration: 2000
			});
		},
		SubmitExamine: function(e) {

			uni.showModal({
				title: '提示',
				content: '确认通过审核？',
				success: (res) => {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.showLoading({
						    title: '加载中'
						});

						const num = this.subjectIndex;
						const sendData = this.subjectList[num];
						sendData.isSubject = true;
						
						uniCloud.callFunction({
							name: 'updataTestSubject',
							data: {
								subject: sendData
							},
							success: (res) => {
								this.ShowToast('上传成功');
								// 审核通过更新视图
								this.ResetExamine(num);
							},
							fail: (res) => {
								console.log('fail:' + res);
								this.ShowToast('上传失败');
							},
							complete: (res) => {
								uni.hideLoading();
								console.log('complete:' + res);
							}
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});

		},
		ResetExamine: function(num) {
			// 删除后数组下标重新排列
			this.subjectList.splice(num, 1);

			if (this.subjectList.length !== 0) {
				// 改变索引（题目显示），当前题型（标题显示）, 刷新页面
				if (this.subjectList.length === num) {
					this.subjectIndex = num - 1;
				}
				this.currentType = this.subjectList[num].type;
			} else {
				this.noSubject = true;
			}

		},
		SwiperChange: function(e) { //滑动事件

			let index = e.target.current;

			// 改变索引（题目显示），当前题型（标题显示）
			if (index != undefined) {
				this.subjectIndex = index;
				this.currentType = this.subjectList[index].type;
			}
		}

	}
}
